﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Database;
using CE.UserAccess;

public partial class User_Account_NewReportsUser : System.Web.UI.Page
{
    string PageCode = "CXTHRNUC";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            ClearProcess();
        }
        if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
        {
            CheckPageAccess();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (CheckDOBValid())
            {
                Account _useraccount = new Account();
                AccountDB _useraccountdb = new AccountDB();

                if (_useraccountdb.IsAlreadyExist(txtEmail.Text.Trim(), 100))
                {
                    lblMessage.Text = "";
                    lblError.Text = "Email address is already Created!";
                    return;
                }
                else
                {
                    lblMessage.Text = "";
                    lblError.Text = "";
                    Int64 _result = -1;

                    _useraccount.UserRole = Convert.ToInt32(ddlUserRole.Text);
                    _useraccount.UserId = 0;
                    _useraccount.Email = txtEmail.Text.ToLower();
                    _useraccount.AuthCode = txtPassword.Text;
                    _useraccount.UserTypeId = 100;
                    _useraccount.Status = true;
                    _useraccount.RefUserId = Convert.ToInt64(Session[General.UserId].ToString());

                    _result = _useraccountdb.Insert(_useraccount);
                    _useraccount.UserId = _result;
                    if (_result > 0)
                    {
                        Profile _profile = new Profile();
                        ProfileDB _profiledb = new ProfileDB();

                        _profile.UserId = _result;
                        _profile.FirstName = CE.Global.General.ClearScriptTag(Server.HtmlEncode(txtFirstName.Text));
                        _profile.SurName = CE.Global.General.ClearScriptTag(Server.HtmlEncode(txtSurName.Text));
                        _profile.DOB = Convert.ToDateTime(Convert.ToInt32(ddlYear.Text) + "/" + Convert.ToInt32(ddlMonth.Text) + "/" + Convert.ToInt32(ddlDay.Text));
                        _profile.Gender = Convert.ToChar(ddlGender.Text);
                        _profile.MaritalStatus = Convert.ToInt32(ddlMaritalStatus.Text);
                        Int64 _profileresult = _profiledb.Insert(_profile);
                        if (_profileresult > 0)
                        {
                            ClearProcess();
                            lblMessage.Text = "Successfully Created!";
                            lblError.Text = "";
                        }
                    }
                }
            }
            else
            {
                lblError.Text = "Invalid DOB";
                return;
            }
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    private void ClearProcess()
    {
        txtEmail.Text = "";
        txtPassword.Text = "";
        txtConfirmPassword.Text = "";
        lblError.Text = "";
        lblMessage.Text = "";
        txtFirstName.Text = "";
        txtSurName.Text = "";
        ddlDay.Items.Clear();
        ddlMonth.Items.Clear();
        ddlYear.Items.Clear();

        int _cryr = CE.Global.General.GetUTCDateTime().Year;
        int _styr = _cryr - 70;
        int _edyr = _cryr - 18;
        ddlDay.Items.Add(new ListItem("dd", "00"));
        ddlMonth.Items.Add(new ListItem("mm", "00"));
        ddlYear.Items.Add(new ListItem("yyyy","0000"));
        for (int _year = _edyr; _year >= _styr; _year--)
        {
            ddlYear.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
        }
        for (int _dd = 1; 31 >= _dd; _dd++)
        {
            string day = _dd.ToString();
            if (_dd.ToString().Length < 2)
            {
                day = "0" + _dd.ToString();
            }
            ddlDay.Items.Add(new ListItem(day, day));
        }
        for (int _mm = 1; 12 >= _mm; _mm++)
        {
            string month = _mm.ToString();
            if (_mm.ToString().Length < 2)
            {
                month = "0" + _mm.ToString();
            }
            ddlMonth.Items.Add(new ListItem(GetMonth(month), month));
        }
    }
    
    public string GetMonth(string month)
    {
        string result = "";
        if (month == "01")
        {
            result = " Jan";
        }
        else if (month == "02")
        {
            result = " Feb";
        }
        else if (month == "03")
        {
            result = " Mar";
        }
        else if (month == "04")
        {
            result = " Apr";
        }
        else if (month == "05")
        {
            result = " May";
        }
        else if (month == "06")
        {
            result = " Jun";
        }
        else if (month == "07")
        {
            result = " Jul";
        }
        else if (month == "08")
        {
            result = " Aug";
        }
        else if (month == "09")
        {
            result = " Sep";
        }
        else if (month == "10")
        {
            result = " Oct";
        }
        else if (month == "11")
        {
            result = " Nov";
        }
        else if (month == "12")
        {
            result = " Dec";
        }
        return result;
    }

    public bool ValidDate(Int32 Year, Int32 Month, Int32 Day)
    {
        bool _isValid = true;
        int daysInMonth = 0;
        string leap, monthValid, dayValid, yearValid;
        if (Year >= 0000)
        {
            yearValid = "yes";
        }
        else
        {
            yearValid = "no";
        }
        if (Year % 400 == 0)
        {
            leap = "yes";
        }
        else if (Year % 100 == 0)
        {
            leap = "no";
        }
        else if (Year % 4 == 0)
        {
            leap = "yes";
        }
        else
        {
            leap = "no";
        }

        switch (Month)
        {
            case 1: daysInMonth = 31;
                break;
            case 3: daysInMonth = 31;
                break;
            case 4: daysInMonth = 30;
                break;
            case 5: daysInMonth = 31;
                break;
            case 6: daysInMonth = 30;
                break;
            case 7: daysInMonth = 31;
                break;
            case 8: daysInMonth = 31;
                break;
            case 9: daysInMonth = 30;
                break;
            case 10: daysInMonth = 31;
                break;
            case 11: daysInMonth = 30;
                break;
            case 12: daysInMonth = 31;
                break;
        }

        if (Month >= 1 && Month <= 12)
        {
            monthValid = "yes";
        }
        else
        {
            monthValid = "no";
        }
        if (Month == 2)
        {
            if (leap == "no")
            {
                daysInMonth = 28;
            }
            else if (leap == "yes")
            {
                daysInMonth = 29;
            }
        }

        if (1 <= Day && Day <= daysInMonth)
        {
            dayValid = "yes";
        }

        else
        {
            dayValid = "no";
        }

        if ((dayValid == "yes") && (monthValid == "yes") && (yearValid == "yes"))
        {
            _isValid = true;
        }
        else
        {
            _isValid = false;
        }

        return _isValid;
    }

    public bool CheckDOBValid()
    {
        bool _Valid = true;
        if (!ValidDate(Convert.ToInt32(ddlYear.Text), Convert.ToInt32(ddlMonth.Text), Convert.ToInt32(ddlDay.Text)))
        {
            _Valid = false;
        }
        return _Valid;
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else if (PageAccess > 0)
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            btnSubmit.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnSubmit.Enabled = true;
        }
    }

}
